package com.example.yls.demoa;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_dog;
    private Button btn_start;
    private Button btn_start_vlaue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_dog=findViewById(R.id.iv_dog);
        btn_start=findViewById(R.id.btn_start);
        btn_start_vlaue=findViewById(R.id.btn_start_value);

        btn_start_vlaue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               ValueAnimator valueAnimator=ValueAnimator.ofInt(btn_start_vlaue.getLayoutParams().width,500);
                valueAnimator.setDuration(3000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int value = (int) valueAnimator.getAnimatedValue();
                        btn_start_vlaue.getLayoutParams().width=value;
                       btn_start_vlaue.setBackgroundColor(Color.argb(155,255,0,0));
                        btn_start_vlaue.requestLayout();
                    }
                });
                valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
                valueAnimator.setRepeatCount(-1);
                valueAnimator.start();

            }
        });
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*startRotateanimation();
                startAnimationSetWithJAVA();*/
                startAnimationSetWithXML();

            }
        });
    }

    private void startAnimationSetWithXML() {
        Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animationset);
        iv_dog.startAnimation(animation);

    }

    private void startAnimationSetWithJAVA() {
        AnimationSet animationSet=new AnimationSet(true);
        RotateAnimation rotateAnimation=new RotateAnimation(0,360,150,150);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatMode(RotateAnimation.REVERSE);

        TranslateAnimation translateAnimation=new TranslateAnimation(0,300,0,300);
        translateAnimation.setDuration(3000);
        translateAnimation.setRepeatCount(-1);
        translateAnimation.setFillAfter(true);
        translateAnimation.setRepeatMode(RotateAnimation.REVERSE);

        ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatCount(-1);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatMode(RotateAnimation.REVERSE);

        AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setRepeatCount(-1);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatMode(RotateAnimation.REVERSE);

    animationSet.addAnimation(rotateAnimation);
    animationSet.addAnimation(translateAnimation);
    animationSet.addAnimation(alphaAnimation);
    animationSet.addAnimation(scaleAnimation);
    iv_dog.startAnimation(animationSet);

    }

    private void startRotateanimation() {
        RotateAnimation rotateAnimation=new RotateAnimation(0,360,150,150);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatMode(RotateAnimation.REVERSE);
        iv_dog.startAnimation(rotateAnimation);
    }
}
